#pragma once
#include <iostream>
#include <functional>
#include <string>
#include "InetAddr.hpp"
#include "reactor.hpp"
// 此文件功能：设置要关心的事件、设置事件处理方法
class connection;
class reactor;
using func_t = std::function<void(connection *)>;
class connection
{
public:
    connection(int sock)
        : _sock(sock), _r(nullptr)
    {
    }
    ~connection()
    {
    }
    void set_event(uint32_t events)
    {
        _events = events;
    }
    void set_self(reactor *r)
    {
        _r = r;
    }
    void record(func_t recver, func_t sender, func_t excepter)
    {
        _recver = recver;
        _sender = sender;
        _excepter = excepter;
    }
    void append_inbuffer(const std::string &buffer)
    {
        _inbuffer += buffer;
    }
    std::string &inbuffer()
    {
        return _inbuffer;
    }
    std::string& outbuffer()
    {
        return _outbuffer;
    }
    bool out_buffer_empty()
    {
        return _outbuffer.empty();
    }
    void out_buffer_remove(int n)
    {
        _outbuffer.erase(0,n);
    }
    void sockfd_close()
    {
        if(_sock > 0)
        {
            ::close(_sock);
        }
    }
    uint16_t events()
    {
        return _events;
    }
    int sockfd()
    {
        return _sock;
    }

private:
    int _sock;
    std::string _inbuffer;
    std::string _outbuffer;
    InetAddr _addr;
    uint32_t _events; // connection对象中，_sock关心的事件集合
public:
    func_t _recver;   // 接受数据
    func_t _sender;   // 发送数据
    func_t _excepter; // 处理错误
    reactor *_r;
};